package cn.zkf.bean.TestAware;

import cn.zkf.bean.User;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @program: spring测试在加载user Bean的时候做其它特殊处理
 * @ClassName LoadUserAware
 * @description:
 * @author: zhangkefei
 * @create: 2021-06-16 17:08
 * @Version 1.0
 **/
@Component
public class LoadUserAware implements InitializingBean, ApplicationContextAware {

	private ApplicationContext applicationContext;
	/**
	 * 实现接口 InitializingBean
	 * bean在创建时的后置处理器
	 * @throws Exception
	 */
	@Override
	public void afterPropertiesSet() throws Exception {
		User user=this.applicationContext.getBean(User.class);
		user.setName("zkfPro");
//		if(user instanceof InitializingBean)
	}

	/**
	 * 实现接口ApplicationContextAware 可以获得ApplicationContext容器，从而获取容器中的Bean
	 * @param applicationContext the ApplicationContext object to be used by this object
	 * @throws BeansException
	 */
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
	}
}
